import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# 假设的参数
np.random.seed(42)  # 为了可重复性
num_simulations = 10000  # 模拟次数
mean_cash_inflow = 1000000000  # 平均现金流入（可以根据实际情况调整）
std_cash_inflow = 100000000  # 现金流入的标准差
mean_cash_outflow = 800000000  # 平均现金流出（可以根据实际情况调整）
std_cash_outflow = 80000000  # 现金流出的标准差

# 生成模拟的现金流入和流出
cash_inflows = np.random.normal(mean_cash_inflow, std_cash_inflow, num_simulations)
cash_outflows = np.random.normal(mean_cash_outflow, std_cash_outflow, num_simulations)

# 计算每次模拟的净现金流量
net_cash_flows = cash_inflows - cash_outflows

# 计算流动性不足的次数（即净现金流为负的次数）
liquidity_shortfalls = np.sum(net_cash_flows < 0)

# 计算流动性风险的概率
probability_of_shortfall = liquidity_shortfalls / num_simulations

# 打印结果
print(f"流动性不足的概率: {probability_of_shortfall:.2%}")

# 绘制净现金流量的直方图
plt.hist(net_cash_flows, bins=50, alpha=0.7, label='Net Cash Flows')
plt.axvline(x=0, color='r', linestyle='--', label='Zero Cash Flow')
plt.title('Net Cash Flows from Monte Carlo Simulation')
plt.xlabel('Net Cash Flow')
plt.ylabel('Frequency')
plt.legend()
plt.show()